<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
//期数管理控制器
class PeriodsController extends CommonController{
	private $model;
	public function __construct(){
		parent::__construct();
		$this->model = new \Common\Model\Periods;
	}
	
	
	//添加期数
	public function add(){
		if(IS_POST){
			if($this->model->addData()){
				View::success('添加成功',U('index'));
			}
			
			View::error($this->model->getError());
		}
		$goodsModel = new \Common\Model\Goods;
		$goodsData = $goodsModel->get();
		
		View::with('goodsData',$goodsData);
		View::make();
	}
	
	//显示期数
	public function index(){
		//获得所有数据
		$data = $this->model->groupBy('periods')->get();
		View::with('data',$data);
	    View::make();
	}
	
	//被异步添加期数请求的方法
	public function periodsNum(){
		//接收异步post过来的输入框里面的数据（期数）
		$p_num = Q('post.p_num');
		
		//1.当输入完之后又删除输入的数字就为空。
		if($p_num == ''){
			return;
		}
		
		//2.
		$preg = '/^\d+$/';
		$bool = preg_match($preg, $p_num);
//		sp($bool);exit;
//   if(!$bool){}
		if($bool == 0){
			echo json_encode(2);exit;
		}
		
		//3.只能从第一期开始   //4.如果数据表期数是空的，显示全部商品
		
		$periodsData = $this->model->field('periods')->get();
		
		$goodsModel = new \Common\Model\Goods;
		if(!$periodsData){
			if($p_num != 1){
				echo json_encode(1);exit;
			}
			$goodsData = $goodsModel->where("gnum>={$p_num} AND gstate=0")->get();
			echo json_encode($goodsData); exit;
			
		}
		
		//5.到现在数据表已经有数据了,判断如果添加过期数的，且期数是按顺序输入的，就会显示。没有添加过输入当前期数之前的期数就不会显示
//		$goodsData = $goodsModel->where("gnum>={$p_num} AND gstate=0")->get();
//		echo json_encode($goodsData); exit;
		
		$num = $p_num - 1;
		if($num == 0){
//			sp(123);exit;
			//如果$num等于0时，让输入1的时候，得到已经添加第一期的goods_gid
			$periodsData = $this->model->where("periods={$p_num}")->lists('goods_gid');
//			sp($periodsData);exit;
			$tempGids = implode(',', $periodsData);
			//显示没有添加第一期的商品
			$goodsData = $goodsModel->where("gnum>={$p_num} AND gid not in($tempGids) AND gstate=0")->get();
//			sp($goodsData);
			echo json_encode($goodsData);exit;
		}
		
		//6.跳期数添加 不可以
		$beforGids = $this->model->where("periods={$num}")->lists('goods_gid');
		if($beforGids == ''){
			echo json_encode('');exit;
		}
		
		//7.挨着期数添加
		$beforGids = implode(',', $beforGids);
//		$goodsData = $goodsModel->where("gnum>={$p_num} AND gid in($beforGids) AND gstate=0")->get();
//		echo json_encode($goodsData);
		
		//8.排除已经添加过期数的商品
		$nowGids = $this->model->where("periods={$p_num}")->lists('goods_gid');
		if($nowGids == ''){
			$goodsData = $goodsModel->where("gnum>={$p_num} AND gid in($beforGids) AND gstate=0")->get();
			echo json_encode($goodsData);exit;
		}
		$nowGids = implode(',', $nowGids);
		$goodsData = $goodsModel->where("gnum>={$p_num} AND gid in($beforGids) AND gid not in($nowGids) AND gstate=0")->get();
		echo json_encode($goodsData);

	}
	
	//异步选择某一期的所有商品
	public function selectPeriods(){
		$pNum = Q('post.pNum');
//		sp($pNum);exit;
		if($pNum){
			$periodsData = $this->model->where("periods={$pNum}")->lists('goods_gid');
//			sp($periodsData);exit;
			$tempGids = implode(',', $periodsData);
			$goodsModel = new \Common\Model\Goods;
			$goodsData = $goodsModel->join('goods_periods','gid','=','goods_gid')->where("gid in($tempGids) AND periods={$pNum}")->get();
//			sp($goodsData);exit;
			echo json_encode($goodsData);
		}
	}
	
	//删除某一商品的期数
	public function del(){
		$this->model->realDel(Q('get.gpid',0,'intval'));
		View::success('删除成功');
	}
}






 ?>